<!DOCTYPE html>


<html lang="zh_CN">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>Zookeeper |  MyBlog</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    <link rel="alternate" href="/atom.xml" title="MyBlog" type="application/atom+xml">
</head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-Zookeeper笔记"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Zookeeper
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2021/12/04/Zookeeper%E7%AC%94%E8%AE%B0/" class="article-date">
  <time datetime="2021-12-04T05:41:04.726Z" itemprop="datePublished">2021-12-04</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.6k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">10 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="一、Zookeeper介绍"><a href="#一、Zookeeper介绍" class="headerlink" title="一、Zookeeper介绍"></a>一、Zookeeper介绍</h1><p>1.什么是Zookeeper</p>
<p>Zookeeper是一种分布式协调服务，用于管理大型主机。</p>
<p>2.Zookeeper的应用场景</p>
<ul>
<li>分布式协调组件</li>
<li>分布式锁</li>
<li>无状态化的实现</li>
</ul>
<h1 id="二、搭建Zookeeper服务器"><a href="#二、搭建Zookeeper服务器" class="headerlink" title="二、搭建Zookeeper服务器"></a>二、搭建Zookeeper服务器</h1><h2 id="1-zookeeper安装"><a href="#1-zookeeper安装" class="headerlink" title="1.zookeeper安装"></a>1.zookeeper安装</h2><p>官网：<a target="_blank" rel="noopener" href="http://zookeeper.apache.org/releases.html">http://zookeeper.apache.org/releases.html</a></p>
<p>1.安装zookeeper.tar.gz</p>
<p>2.在linux中/usr/lcoal新建zookeeper目录，然后通过xftp将压缩包传送到虚拟机中，再使用tar命令解压</p>
<h2 id="2-zoo-cfg配置文件说明"><a href="#2-zoo-cfg配置文件说明" class="headerlink" title="2.zoo.cfg配置文件说明"></a>2.zoo.cfg配置文件说明</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># zookeeper时间配置中的基本单位（毫秒）</span></span><br><span class="line">tickTime=2000</span><br><span class="line"><span class="comment"># 允许follower初始化连接到leader最大时长，它表示tickTime时间倍数。即：initLimit*tickTime</span></span><br><span class="line">initLimit=10</span><br><span class="line"><span class="comment"># 允许follower与leader数据同步最大时长，它表示tickTime时间倍数</span></span><br><span class="line">syncLimit=5</span><br><span class="line"><span class="comment"># zookeeper数据存储目录及日志保存目录（如果没有指明dataLogDir，则日志也保存再这个文件中）</span></span><br><span class="line">dataDir=/tmp/zookeeper</span><br><span class="line"><span class="comment">#对客户端提供的端口号</span></span><br><span class="line">clientPort=2181</span><br><span class="line"><span class="comment">#单个客户端与zookeeper最大并发连接数</span></span><br><span class="line">maxClientCnxns=60</span><br><span class="line"><span class="comment">#保存的数据快照数量，之外的将被清除</span></span><br><span class="line">autopurge.snapRetainCount=3</span><br><span class="line"><span class="comment">#自动触发清除任务时间间隔，小时为单位。默认为0，表示不自动清除</span></span><br><span class="line">autopurge.purgeInterval=1</span><br></pre></td></tr></table></figure>

<h2 id="3-Zookeeper服务器的操作命令"><a href="#3-Zookeeper服务器的操作命令" class="headerlink" title="3.Zookeeper服务器的操作命令"></a>3.Zookeeper服务器的操作命令</h2><ul>
<li>重命名conf中的文件zoo_sample.cfg  –&gt;  zoo.cfg</li>
<li>启动zk服务器</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bin/zkServer.sh start ./conf/zoo.cfg</span><br></pre></td></tr></table></figure>

<ul>
<li>查看zk服务器状态</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bin/zkServer.sh status ./conf/zoo.cfg</span><br></pre></td></tr></table></figure>

<ul>
<li>停止zk服务器：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.bin/zkServer.sh stop ./conf/zoo.cfg</span><br></pre></td></tr></table></figure>

<h1 id="三、Zookeeper内部数据模型"><a href="#三、Zookeeper内部数据模型" class="headerlink" title="三、Zookeeper内部数据模型"></a>三、Zookeeper内部数据模型</h1><h2 id="1-zk是如何保存数据的"><a href="#1-zk是如何保存数据的" class="headerlink" title="1.zk是如何保存数据的"></a>1.zk是如何保存数据的</h2><blockquote>
<p>zookeeper命令zkCli.sh登录客户端</p>
</blockquote>
<p>zk中的数据是保存在节点上的，节点就是znode,多个znode之间构成一颗树的目录结构</p>
<p>Zookeeper的数据模型就像数据结构中的树，也很像文件系统的的目录</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112030926383.png" alt="image-20211202114933945"></p>
<p>树是由节点所组成，Zookeeper的数据存储也同样是基于节点，这种节点叫做Znode</p>
<p>但是，不同树的结点，Znode的引用方式是路径引用，类似于文件路径：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">/动物/猫</span><br><span class="line">/汽车/宝马</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">create /<span class="built_in">test</span>  创建<span class="built_in">test</span>结点</span><br><span class="line"></span><br><span class="line">create /<span class="built_in">test</span>  abc  创建<span class="built_in">test</span>并赋值abc</span><br><span class="line"></span><br><span class="line">create /<span class="built_in">test</span>/sub  在<span class="built_in">test</span>节点下再创建sub节点</span><br></pre></td></tr></table></figure>

<h2 id="2-zk中的znode是什么样的结构"><a href="#2-zk中的znode是什么样的结构" class="headerlink" title="2.zk中的znode是什么样的结构"></a>2.zk中的znode是什么样的结构</h2><p>zk中的znode，包含四个部分：</p>
<ul>
<li>data：保存数据</li>
<li>acl：权限，定义了什么样的用户能够操作这个节点，且能够进行怎样的操作<ul>
<li>c：create创建权限，允许再该节点、下创建节点</li>
<li>w：write更新权限，允许更新该节点的数据</li>
<li>r：read读取权限，允许读取该节点的内容以及子节点的列表信息</li>
<li>d：delete删除权限，允许删除该节点的子节点</li>
<li>a：admin管理者权限，允许对该节点</li>
</ul>
</li>
<li>stat：描述当前znode的元数据  get -s 节点</li>
<li>child：当前节点的子节点</li>
</ul>
<h2 id="3-zk中节点znode的类型"><a href="#3-zk中节点znode的类型" class="headerlink" title="3.zk中节点znode的类型"></a>3.zk中节点znode的类型</h2><ul>
<li>持久节点：创建出的节点，再会话结束后依然存在在。保存数据</li>
<li>持久序号节点：创建出的节点，根据先后顺序，会在节点之后带上一个数值，越后执行数值越大，适用于分布式锁的应用场景，单调递增     create -s 节点</li>
<li>临时节点： create -e 节点</li>
</ul>
<p>​        临时节点是在会话结束后，自动被删除的，通过这个给特性，zk可以实现服务注册与发现的效果。那么临时节点是如何维持心跳呢？</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112021904252.png" alt="image-20211202190445705"></p>
<h2 id="4-zk的数据持久化"><a href="#4-zk的数据持久化" class="headerlink" title="4.zk的数据持久化"></a>4.zk的数据持久化</h2><p>zk的数据是运行在内存中，zk提供了两种持久化机制：</p>
<ul>
<li>事务日志</li>
<li>数据快照</li>
</ul>
<p>zk通过两种形式的持久化，在恢复时先恢复快照文件中的数据到内存中，再用日志文件中的数据做增量恢复，这样的恢复速度更快</p>
<h1 id="四、Zookeeper客户端zkCli的使用"><a href="#四、Zookeeper客户端zkCli的使用" class="headerlink" title="四、Zookeeper客户端zkCli的使用"></a>四、Zookeeper客户端zkCli的使用</h1><h2 id="1-查询节点"><a href="#1-查询节点" class="headerlink" title="1.查询节点"></a>1.查询节点</h2><p>ls -R 节点：递归查询某节点下的子节点</p>
<p>get -s 节点：查看节点详细信息</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112030925840.png" alt="image-20211202164833229"></p>
<h2 id="2-删除节点"><a href="#2-删除节点" class="headerlink" title="2.删除节点"></a>2.删除节点</h2><ul>
<li>普通删除</li>
<li>乐观锁删除   根据版本号（dataVersion）删除    ==&gt;  delete  -v  1  节点</li>
</ul>
<h2 id="3-权限"><a href="#3-权限" class="headerlink" title="3.权限"></a>3.权限</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">addauth digest zhangsan:123   #添加权限</span><br><span class="line">create /test-node abc auth:zhangsan:123:crwda  #指定节点需要某种权限才可访问</span><br></pre></td></tr></table></figure>

<h1 id="五、Curator客户端的使用"><a href="#五、Curator客户端的使用" class="headerlink" title="五、Curator客户端的使用"></a>五、Curator客户端的使用</h1><h2 id="1-Curator介绍"><a href="#1-Curator介绍" class="headerlink" title="1.Curator介绍"></a>1.Curator介绍</h2><p>Curator是Netflix公司开源的一套zookeeper客户端框架，Curator是对Zookeeper支持最好的客户端框架。封装了大部分Zookeeper功能。比如Leader选举、分布式锁等</p>
<h3 id="1-1引入Curator"><a href="#1-1引入Curator" class="headerlink" title="1.1引入Curator"></a>1.1引入Curator</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.projectlombok<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lombok<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.zookeeper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zookeeper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.5.9<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-log4j12<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-framework<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.zookeeper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zookeeper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-recipes<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.3.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.junit.vintage<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit-vintage-engine<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependencyManagement</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-dependencies<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.6.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">type</span>&gt;</span>pom<span class="tag">&lt;/<span class="name">type</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">scope</span>&gt;</span>import<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependencyManagement</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="1-2application-properties配置文件"><a href="#1-2application-properties配置文件" class="headerlink" title="1.2application.properties配置文件"></a>1.2application.properties配置文件</h3><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">curator.retryCount</span>=<span class="string">5</span></span><br><span class="line"><span class="meta">curator.elapsedTimeMs</span>=<span class="string">5000</span></span><br><span class="line"><span class="meta">curator.connectString</span>=<span class="string">192.168.227.20:2181</span></span><br><span class="line"><span class="meta">curator.sessionTimeoutMs</span>=<span class="string">60000</span></span><br><span class="line"><span class="meta">curator.connectionTimeoutMs</span>=<span class="string">5000</span></span><br></pre></td></tr></table></figure>

<h3 id="1-3-ConfigurationProperties"><a href="#1-3-ConfigurationProperties" class="headerlink" title="1.3@ConfigurationProperties"></a>1.3@ConfigurationProperties</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@ConfigurationProperties(prefix = &quot;curator&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WrapperZK</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> retryCount;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> elapsedTimeMs;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> String connectString;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> sessionTimeoutMs;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> connectionTimeoutMs;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="六、zk实现分布式锁"><a href="#六、zk实现分布式锁" class="headerlink" title="六、zk实现分布式锁"></a>六、zk实现分布式锁</h1><h2 id="1-zk’中锁的种类"><a href="#1-zk’中锁的种类" class="headerlink" title="1.zk’中锁的种类"></a>1.zk’中锁的种类</h2><ul>
<li>读锁：都可以读，要上读锁的前提：之前的锁没有写锁</li>
<li>写锁：只有得到写锁才能写。要想上写锁的前提是，之前没有任何锁</li>
</ul>
<h2 id="2-zk如何上读锁"><a href="#2-zk如何上读锁" class="headerlink" title="2.zk如何上读锁"></a>2.zk如何上读锁</h2><ul>
<li><p>创建一个临时序号节点，节点的数据是read，表示读锁</p>
</li>
<li><p>获取当前zk中序号比自己小的所有节点</p>
</li>
<li><p>判断最小节点是否是读锁</p>
<ul>
<li>如果不是读锁的话，则上锁失败，为最小节点设置监听。阻塞等待，zk的watch机制会当最小节点发生变化时通知当前节点，于是再执行第二步的流程</li>
<li>如果时读锁的话，则上锁成功</li>
</ul>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112030925665.png" alt="image-20211203092536781"></p>
</li>
</ul>
<h2 id="3-zk如何上写锁"><a href="#3-zk如何上写锁" class="headerlink" title="3.zk如何上写锁"></a>3.zk如何上写锁</h2><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112201717037.png" alt="image-20211203092658169"></p>
<h2 id="4-羊群效应"><a href="#4-羊群效应" class="headerlink" title="4.羊群效应"></a>4.羊群效应</h2><p>如果用上述的上锁方式，只要有节点发生变化，就会触发其他节点也监听时间，这样的话对zk的压力非常大——羊群效应。可以调整成链式监听，解决这个问题。</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112030935233.png" alt="image-20211203093529455"></p>
<h1 id="七、Watch机制"><a href="#七、Watch机制" class="headerlink" title="七、Watch机制"></a>七、Watch机制</h1><h2 id="监听节点"><a href="#监听节点" class="headerlink" title="监听节点"></a>监听节点</h2><p>当节点发生改变，即调用了create，delete，setData方法的时候，将会触发znode上注册的对应时间，球球watch的客户端会接收到异步通知</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">get -w 节点     <span class="comment"># 监听是一次性的</span></span><br><span class="line">ls -w 节点      <span class="comment"># 对节点目录的监听</span></span><br><span class="line">ls -R -w 节点   <span class="comment"># 递归监听</span></span><br></pre></td></tr></table></figure>

<h1 id="八、Zookeeper集群实战"><a href="#八、Zookeeper集群实战" class="headerlink" title="八、Zookeeper集群实战"></a>八、Zookeeper集群实战</h1><h2 id="1-Zookeeper集群角色"><a href="#1-Zookeeper集群角色" class="headerlink" title="1.Zookeeper集群角色"></a>1.Zookeeper集群角色</h2><p>zookeeper集群中的节点有三种角色</p>
<ul>
<li>leader</li>
<li>follower</li>
<li>observer</li>
</ul>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112031030252.png" alt="image-20211203103045779"></p>
<h2 id="2-集群搭建"><a href="#2-集群搭建" class="headerlink" title="2.集群搭建"></a>2.集群搭建</h2><p>2.1在zkdata中创建四个目录zk1,zk2,zk3,zk4</p>
<p>2.2在目录中新建myid文件，使用vim填写它们的id （表示当前服务节点的唯一标识</p>
<p>或者使用 echo 1 &gt; ./zk1/myid</p>
<p>2.3在conf目录下cp zoo.cfg zoo1.cfg</p>
<p>2.4在拷贝的文件中修改端口号，持久化文件以及日志文件所保存的位置（2.1新建的目录位置）</p>
<p>2.5设置集群间的通信端口的选举端口</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 2001为集群通信端口，3001为集群选举端口，observer表示不参与集群选举</span></span><br><span class="line">server.1=192.168.227.20:2001:3001</span><br><span class="line">server.2=192.168.227.20:2002:3002</span><br><span class="line">server.3=192.168.227.20:2003:3003</span><br><span class="line">server.4=192.168.227.20:2004:3004:observer</span><br></pre></td></tr></table></figure>

<p>2.6启动四台zookeeper</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bin/zkServer.sh start ./conf/zoo1.cfg</span><br></pre></td></tr></table></figure>



<h2 id="3-连接Zookeeper集群"><a href="#3-连接Zookeeper集群" class="headerlink" title="3.连接Zookeeper集群"></a>3.连接Zookeeper集群</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bin/zkCli.sh -server 192.168.227.20:2181,192.168.227.20:2182,192.168.227.20:2183</span><br></pre></td></tr></table></figure>

<h1 id="九、ZAB协议"><a href="#九、ZAB协议" class="headerlink" title="九、ZAB协议"></a>九、ZAB协议</h1><h2 id="1-什么是ZAB协议"><a href="#1-什么是ZAB协议" class="headerlink" title="1.什么是ZAB协议"></a>1.什么是ZAB协议</h2><p>Zookeeper为了保证数据的一致性，使用了ZAB（Zookeeper Atomic Broadcast）协议，这个协议解决了Zooeeper的崩溃恢复和主从数据同步的问题</p>
<h2 id="2-ZAB协议定义的四种节点状态"><a href="#2-ZAB协议定义的四种节点状态" class="headerlink" title="2.ZAB协议定义的四种节点状态"></a>2.ZAB协议定义的四种节点状态</h2><ul>
<li>Looking：选举状态</li>
<li>Following：Follower节点（从节点）所处的状态</li>
<li>Leading：Leader节点（主节点）所处状态</li>
<li>Observing：观察者节点所处的状态</li>
</ul>
<h2 id="3-集群上线时的Leader选举过程"><a href="#3-集群上线时的Leader选举过程" class="headerlink" title="3.集群上线时的Leader选举过程"></a>3.集群上线时的Leader选举过程</h2><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112031130960.png" alt="image-20211203113039474"></p>
<p>zXid：事务id（只要做任何增删改就会+1）</p>
<h2 id="4-崩溃恢复时的leader选举"><a href="#4-崩溃恢复时的leader选举" class="headerlink" title="4.崩溃恢复时的leader选举"></a>4.崩溃恢复时的leader选举</h2><p>Leader建立完后，Leader周期性地不断向Follower发送心跳（ping命令，没有内容的socket）。当leader崩溃后，Follower发现socket通道已关闭，于是从机从follower状态转为looking状态</p>
<h2 id="5-主从复制原理"><a href="#5-主从复制原理" class="headerlink" title="5.主从复制原理"></a>5.主从复制原理</h2><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112031607676.png" alt="image-20211203160732037"></p>
<p>半数以上的ACK是指整个集群半数以上</p>
<p><em>ACK</em> (<em>Ack</em>nowledge character）即是确认字符，在数据通信中，接收站发给发送站的一种传输类控制字符。表示发来的数据已确认接收无误。</p>
<h2 id="6-Zookeeper中的NIO与BIO的应用"><a href="#6-Zookeeper中的NIO与BIO的应用" class="headerlink" title="6.Zookeeper中的NIO与BIO的应用"></a>6.Zookeeper中的NIO与BIO的应用</h2><ul>
<li>NIO<ul>
<li>用于被客户端连接的2181端口，使用的是NIO模式与客户端建立连接</li>
<li>客户端开启Watch时，也使用NIO，等待Zookeeper服务器的回调</li>
</ul>
</li>
<li>BIO<ul>
<li>集群在选举时，多个节点之间的投票通信端口，使用BIO进行通信</li>
</ul>
</li>
</ul>
<h1 id="十、CAP理论"><a href="#十、CAP理论" class="headerlink" title="十、CAP理论"></a>十、CAP理论</h1><h2 id="1-CAP定理"><a href="#1-CAP定理" class="headerlink" title="1.CAP定理"></a>1.CAP定理</h2><p>CAP理论为：一个分布式系统最多只能同时满足一致性（Consistency）、可用性（Availability）和分区容错性（Partition tolerance）这三项中的两项。</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112032132913.png" alt="image-20211203213235768"></p>
<h2 id="2-理解CAP定理"><a href="#2-理解CAP定理" class="headerlink" title="2.理解CAP定理"></a>2.理解CAP定理</h2><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112032157992.png" alt="image-20211203215712227"></p>
<h2 id="3-BASE理论"><a href="#3-BASE理论" class="headerlink" title="3.BASE理论"></a>3.BASE理论</h2><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112032158346.png" alt="image-20211203215834647"></p>
<h2 id="4-Zookeeper追求的一致性"><a href="#4-Zookeeper追求的一致性" class="headerlink" title="4.Zookeeper追求的一致性"></a>4.Zookeeper追求的一致性</h2><p>Zookeeper在数据同步时，追求的并不是强一致性，而是顺序一致性（事务id的单调递增）</p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2021/12/04/Zookeeper%E7%AC%94%E8%AE%B0/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/12/04/%E5%AE%89%E5%85%A8%E4%B8%AD%E9%97%B4%E4%BB%B6/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Spring Security
          
        </div>
      </a>
    
    
      <a href="/2021/12/04/Nginx%E7%AC%94%E8%AE%B0/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Nginx基本概念</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2022
        <i class="ri-heart-fill heart_icon"></i> Yuan
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="MyBlog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->
 
<script src="/js/clickLove.js"></script>
 
<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    <div id="music">
    
    
    
    <iframe frameborder="no" border="1" marginwidth="0" marginheight="0" width="200" height="52"
        src="//music.163.com/outchain/player?type=2&id=22707008&auto=1&height=32"></iframe>
</div>

<style>
    #music {
        position: fixed;
        right: 15px;
        bottom: 0;
        z-index: 998;
    }
</style>
    
    

  </div>
</body>

</html>